package com.example.demo.controller;

import com.example.demo.entity.Contract;
import com.example.demo.entity.Designer;
import com.example.demo.entity.Plan;
import com.example.demo.service.ContractService;
import com.example.demo.service.CustomerService;
import com.example.demo.service.PlanService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/plan")
public class PlanController {
    @Resource
    private PlanService planService;
    @Resource
    private ContractService contractService;

    @RequestMapping("")
    public String showPlan(HttpSession session){
        Designer designer =(Designer) (session.getAttribute("designer"));
        if(designer==null){
            return "login";
        }
        return "planList";
    }

    @RequestMapping(value = "/planList",method = RequestMethod.POST)
    @ResponseBody
    private Map planList(HttpSession session){
        Map map = new HashMap();
        Designer designer =(Designer) (session.getAttribute("designer"));
        String desId = designer.getDesId();
        List<Plan> planList = planService.getAllPlanByDesId(desId);
        if(planList==null || planList.size()==0){
            map.put("msg",0);
        } else {
            map.put("msg",1);
            map.put("planList",planList);
        }
        return map;
    }

    @RequestMapping(value = "planAdd",method = RequestMethod.GET)
    private String planAdd(HttpSession session){
        Designer designer =(Designer) (session.getAttribute("designer"));
        if(designer==null){
            return "login";
        }
        return "planAdd";
    }

    @RequestMapping(value = "/planAdd",method = RequestMethod.POST)
    @ResponseBody
    private Integer planAdd(Plan plan){
        Integer rel = planService.addPlan(plan);
        return rel;
    }

    @RequestMapping(value = "/planDetail/{planId}",method = RequestMethod.GET)
    private String planDetail(@PathVariable(name = "planId")Integer planId, HttpSession session){
        Designer designer =(Designer) (session.getAttribute("designer"));
        if(designer==null){
            return "login";
        } else {
            Plan plan = planService.getPlanById(planId);
            session.setAttribute("plan",plan);
            return "planDetail";
        }
    }

    @RequestMapping(value = "/planDetail",method = RequestMethod.GET)
    @ResponseBody
    private Plan planDetail(HttpSession session){
        Plan plan =(Plan) (session.getAttribute("plan"));
        return plan;
    }

    @RequestMapping(value = "/planModify",method = RequestMethod.POST)
    @ResponseBody
    private Integer planModify(Plan plan){
        Integer rel = planService.modifyPlanById(plan);
        return rel;
    }

    @RequestMapping("/myPlan/{userId}")
    public String myPlan(@PathVariable(name = "userId")Integer userId,HttpSession session){
        Contract myContract = contractService.getContractByUserId(userId);
        session.setAttribute("myContract",myContract);
        return "myPlan";
    }

    @RequestMapping("/getPlanByConCode")
    @ResponseBody
    public Plan getPlanByConCode(String conCode,HttpSession session){
        Contract myContract =(Contract) session.getAttribute("myContract");
        Plan myPlan = planService.getPlanByConCode(myContract.getConCode());
        return myPlan;
    }

    @RequestMapping("/mysearch")
    @ResponseBody
    public List<Plan> mysearch(String keyword,HttpSession session){
        Designer designer =(Designer) (session.getAttribute("designer"));
        List<Plan> planList = planService.getPlan(keyword,designer.getDesId());
        return planList;
    }
}
